package com.framework.network.manager

import com.framework.network.callbacks.OnAddHeadDynamicParamsListener
import com.framework.network.callbacks.OnNetworkResultListener

/**
 * @ClassName: NetworkHelp
 * @Description:
 * @Author: you
 */
class NetworkHelp {
    //最基本的baseUrl
    var baseUrl: String? = null

    //注入的ApiService
    var classType: Class<*>? = null

    //域名集合
    var map: MutableMap<String, String>? = null

    //接口请求的公共参数
    var commonMap: MutableMap<String, Any>? = null

    //头部公共参数
    var headCommonMap: MutableMap<String, Any>? = null

    //监听
    var mOnNetworkResultListener: OnNetworkResultListener? = null

    //头部动态参数
    var mOnAddHeadDynamicParamsListener: OnAddHeadDynamicParamsListener? = null

    //是否开启日志输出
    var isOpenLog = true

    //日志TAG
    var logTag = "net_request"

    /**
     * 是否开启日志输出
     */
    fun isOpenLog(isOpenLog: Boolean, logTag: String = "net_request"): NetworkHelp {
        this.isOpenLog = isOpenLog
        this.logTag = logTag
        return this
    }

    /**
     * 设置BaseUrl
     */
    fun addBaseUrl(baseUrl: String): NetworkHelp {
        this.baseUrl = baseUrl
        return this
    }

    /**
     * 设置注入的ApiService
     */
    fun addClassType(classType: Class<*>): NetworkHelp {
        this.classType = classType
        return this
    }

    /**
     * 设置公共参数
     */
    fun addCommonParams(commonMap: MutableMap<String, Any>): NetworkHelp {
        this.commonMap = commonMap
        return this
    }

    /**
     * 添加头部公共参数
     */
    fun addHeadParams(headCommonMap: MutableMap<String, Any>): NetworkHelp {
        this.headCommonMap = headCommonMap
        return this
    }

    /**
     * 添加头部动态参数监听
     */
    fun setOnAddHeadDynamicParamsListener(listener: OnAddHeadDynamicParamsListener): NetworkHelp {
        this.mOnAddHeadDynamicParamsListener = listener
        return this
    }

    /**
     * 添加域名
     */
    fun addDomainToMap(map: MutableMap<String, String>): NetworkHelp {
        this.map = map
        return this
    }

    /**
     * 设置网络请求的各种监听
     */
    fun setOnNetworkResultListener(onNetworkResultListener: OnNetworkResultListener): NetworkHelp {
        this.mOnNetworkResultListener = onNetworkResultListener
        return this
    }

    /**
     * 找到域名
     */
    fun findDomain(key: String?): String? {
        if (map?.isNullOrEmpty() == false) {
            return map?.get(key)
        }
        return ""
    }

    /**
     * 创建Help对象完成
     */
    fun build(): NetworkManager {
        return NetworkManager.getInstance()
    }
}